local more1__fenwei = fk.CreateSkill({
  name = "more1__fenwei",
  tags = { Skill.Switch },
})

Fk:loadTranslationTable{
  ["more1__fenwei"] = "奋威",
  [":more1__fenwei"] = "转换技，其他角色回合结束时，你可以调整阴阳状态。"..
  "任意角色使用基本牌或锦囊牌时：阴，若你不是使用者则你可以弃置1张花色相同的牌并令此牌无效，然后可以使用此牌(无视距离且不计次限)；"..
  "阳:若此牌指定的目标数大于1，则你令此牌对任意个目标无效；为1，你可以为此牌增加1个目标。",
  --[[["#xiongni-discard"] = "凶逆：弃置一张%arg牌，否则 %src 对你造成1点伤害！",
  prompt = "#xiongni-discard:"..player.id.."::"..suit,]]
  ["#more1__fenwei-choice"] = "选择奋威的阴阳状态",
  ["#more1__fenwei-discard"] = "奋威：你可以弃置一张牌，令此牌无效，然后你使用之！",
  ["#more1__fenwei_use"] = "奋威：你视为使用此牌！",--
  ["#more1__fenwei-choose1"] = "奋威：你可以令此牌对任意个目标无效",--
  ["#more1__fenwei-choose2"] = "奋威：你可以为此牌增加一个目标",--1
  ["$more1__fenwei1"] = "疆场杀敌，勇者，先胜三分!",
  ["$more1__fenwei2"] = "锦帆游侠的各号，岂是自叫的?",
}
local U = require "packages.utility.utility"
more1__fenwei:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(more1__fenwei.name) and (data.card.type == Card.TypeBasic or data.card.type == Card.TypeTrick) and player:getSwitchSkillState(more1__fenwei.name) == fk.SwitchYin 
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if #room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = more1__fenwei.name,
            cancelable = true,
            pattern = ".|.|"..data.card:getSuitString(),
            prompt = "#more1__fenwei-discard",
          }) > 0 then 
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      data.toCard = nil
      data:removeAllTargets()
      if player:canUse(data.card) then

        room.logic:getCurrentEvent():addCleaner(function()
        if not player:isAlive() then return end
        local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if not e then return end
        local use = e.data
        local ids = table.filter(Card:getIdList(use.card), function (id)
          return room:getCardArea(id) == Card.Processing
        end)
        if #ids == 0 then return end
        room:askToUseRealCard(
          player,
          {
            pattern = ids,
            skill_name = more1__fenwei.name,
            prompt = "#more1__fenwei_use",--
            extra_data = {
              bypass_times = true,
              extraUse = true,
              expand_pile = ids,
            },
          }
        )
      end)
    end
  end,
})

more1__fenwei:addEffect(fk.TargetConfirming, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(more1__fenwei.name) and (data.card.type == Card.TypeBasic or data.card.type == Card.TypeTrick) and player:getSwitchSkillState(more1__fenwei.name) == fk.SwitchYang
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
      local targets = data.use.tos
      if #targets > 1 then 
        local tos = room:askToChoosePlayers(player, {
          targets = targets,
          min_num = 1,
          max_num = #targets,
          prompt = "#more1__fenwei-choose1",
          skill_name = more1__fenwei.name,
          cancelable = true,
        })
        if #tos > 0 then
          room:sortByAction(tos)
          event:setCostData(self, {tos = tos})
          return true
        end
      elseif #targets == 1 and #room:getOtherPlayers(targets[1]) > 0  then 
        local tos = room:askToChoosePlayers(player, {
          targets = room:getOtherPlayers(targets[1]),
          min_num = 1,
          max_num = 1,
          prompt = "#more1__fenwei-choose2",--..data.card:toLogString(),
          skill_name = more1__fenwei.name,
          cancelable = true,
        })
        if #tos > 0 then
          event:setCostData(self, {tos = tos})
          return true
        end
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
      if #data.use.tos > 1 then 
        data.use.nullifiedTargets = data.use.nullifiedTargets or {}
        table.insertTable(data.use.nullifiedTargets, event:getCostData(self).tos)
      elseif #data.use.tos == 1 then
        local to = event:getCostData(self).tos[1]
        data:addTarget(to)--
      end
  end,
})







more1__fenwei:addEffect(fk.GameStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(more1__fenwei.name, true)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local skillName = more1__fenwei.name
    local room = player.room
    local choice = room:askToChoice(
      player,
      {
        choices = { "yang", "yin" },
        skill_name = skillName,
        prompt = "#more1__fenwei-choice",
      }
    )
    -- U.SetSwitchSkillState(player, skillName, choice:endsWith("yang") and fk.SwitchYang or fk.SwitchYin)
    choice = choice:endsWith("yang") and fk.SwitchYang or fk.SwitchYin
    U.SetSwitchSkillState(player, more1__fenwei.name, choice)

  end,
})
more1__fenwei:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(more1__fenwei.name, true) and target ~= player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local skillName = more1__fenwei.name
    local room = player.room
    local choice = room:askToChoice(
      player,
      {
        choices = { "yang", "yin" },
        skill_name = skillName,
        prompt = "#more1__fenwei-choice",
      }
    )
    -- U.SetSwitchSkillState(player, skillName, choice:endsWith("yang") and fk.SwitchYang or fk.SwitchYin)
    choice = choice:endsWith("yang") and fk.SwitchYang or fk.SwitchYin
    U.SetSwitchSkillState(player, more1__fenwei.name, choice)

  end,
})

return more1__fenwei
